package cc.comac.mom.controller;

import java.io.BufferedOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.io.IOUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.io.ByteArrayResource;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RequestPart;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import cc.comac.mom.api.EquipDocumentManagementService;
import cc.comac.mom.api.QualityAttachmentService;
import cc.comac.mom.bean.EquipDocumentManagement;
import cc.comac.mom.bean.FileChunkInfo;
import cc.comac.mom.bean.ResultDev;
import cc.comac.mom.config.OSSService;
import cc.comac.mom.config.UploadPreviewData;
import cc.comac.mom.entity.EquipDocumentManagementEntity;
import cc.comac.mom.util.ResultBean;

@RestController
@RequestMapping("/EquipDocumentManagement")
public class EquipDocumentManagementController {

	@Autowired
	private EquipDocumentManagementService equipDocumentManagementService;

	@Autowired
	private QualityAttachmentService qualityAttachmentService;

	@Autowired
	private OSSService oSSService;
	@RequestMapping("/upload")
	public ResultBean upload(@RequestPart("files") MultipartFile files, HttpServletResponse response,
			HttpServletRequest request, EquipDocumentManagement equipDocumentManagement, FileChunkInfo chunkInfo) {
		MultipartFile[] file = new MultipartFile[] {files};
		ResultBean result = qualityAttachmentService.uploadFile(file);
		return result;
	}

	@RequestMapping(value = "/batchSave", method = RequestMethod.POST)
	public ResultBean batchSave(HttpServletResponse response, HttpServletRequest request,
			@RequestBody List<EquipDocumentManagement> list) {
		ResultBean batchSave = equipDocumentManagementService.batchSave(list);
		return batchSave;
	}

	/*@GetMapping(value = "/previewPdf")
	public ResultBean previewPdf(HttpServletRequest request, HttpServletResponse response, @RequestParam long id) {
		byte[] previewPdf = equipDocumentManagementService.previewPdf(id);
		try {
			response.getOutputStream().write(previewPdf);
		} catch (IOException e) {
			e.printStackTrace();
		}
		return null;
	}*/

	@GetMapping(value = "/previewPdf",produces = MediaType.APPLICATION_PDF_VALUE)
	public ResponseEntity<Object> previewPdf(HttpServletRequest request, HttpServletResponse response, @RequestParam long id)  {
		EquipDocumentManagementEntity selectByDocumentId = equipDocumentManagementService
				.selectByDocumentId(id);
			try {
				// 以流的形式下载文件。
				InputStream fis=oSSService.getFileByPathFromOss(selectByDocumentId.getDoucumentRealPath());

				ByteArrayResource bar = new ByteArrayResource(IOUtils.toByteArray(fis));

				return ResponseEntity.ok()
						.header("Content-Disposition", "inline;filename=" +selectByDocumentId.getDocumentName() + "")
						//.contentType(MediaType.APPLICATION_PDF).contentType(MediaType.IMAGE_PNG).contentType(MediaType.IMAGE_JPEG)
						.contentType(MediaType.APPLICATION_PDF)
						.contentLength(bar.contentLength())
						.body(bar);

			} catch (Exception e) {
				e.printStackTrace();
			}
		return null;
	}

	@GetMapping("/download/{documentId}")
	public ResultBean download(@PathVariable("documentId") Long documentId, HttpServletResponse response)  {
		ResultBean resultBean = new ResultBean(true);
		EquipDocumentManagementEntity selectByDocumentId = equipDocumentManagementService
				.selectByDocumentId(documentId);
		
		/*UploadPreviewData.download(selectByDocumentId.getDoucumentRealPath(), selectByDocumentId.getDocumentName(),
				response);*/
		try {
			oSSService.downFileFromOSS(response, selectByDocumentId.getDoucumentRealPath(), selectByDocumentId.getDocumentName());
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			throw new RuntimeException(e);
		}
		return resultBean;
	}

	@RequestMapping(value = "/batchDelete", method = RequestMethod.POST)
	public ResultBean batchDelete(@RequestBody List<EquipDocumentManagement> list) {
		ResultBean batchDelete = equipDocumentManagementService.batchDelete(list);
		return batchDelete;
	}

	@RequestMapping(value = "/selectAllByPage", method = RequestMethod.POST)
	public ResultBean selectAllByPage(@RequestBody EquipDocumentManagement equipDocumentManagement) {
		ResultBean resultBean = new ResultBean(true);
		ResultDev selectAllByPage = equipDocumentManagementService.selectAllByPage(equipDocumentManagement);
		resultBean.setObject(selectAllByPage);
		return resultBean;
	}
}
